<?php

namespace plugin\admin\app\controller;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use plugin\admin\app\model\Activity;
use support\Request;
use support\Response;
use plugin\admin\app\model\ActivityUser;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 报名列表 
 */
class ActivityUserController extends Crud
{
    
    /**
     * @var ActivityUser
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new ActivityUser;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('activity-user/index');
    }

    public function export(Request $request)
    {
        $query = ActivityUser::getQuery();

        if ($request->get('activity_id')) {
            $query->where('activity_id', $request->get('activity_id'));
        }

        if (($name = $request->get('name')) && $name[1] != '') {
            $query->where('name', 'like', "%{$name[1]}%");
        }

        $list = $query->get()->toArray();

        $activityIds = array_column($list, 'activity_id');
        $activities = Activity::whereIn('id', $activityIds)->get()->toArray();
        $activities = array_combine(
            array_column($activities, 'id'),
            array_values($activities)
        );

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '活动名称');
        $sheet->setCellValue('C1', '姓名');
        $sheet->setCellValue('D1', '手机号');
        $sheet->setCellValue('E1', '性别');
        $sheet->setCellValue('F1', '身份证号');

        $row = 1;

        foreach ($list as $key=>$item) {
            $item = json_decode(json_encode($item), true);
            $column = 'A';
            $row++;

            $item['activity'] = $activities[$item['activity_id']];

            $sheet->setCellValue($column . $row, $item['id']);

            $column = chr(ord($column) + 1);

            $sheet->setCellValue($column . $row, $item['activity']['title']);

            $column = chr(ord($column) + 1);

            $sheet->setCellValue($column . $row, $item['name']);

            $column = chr(ord($column) + 1);

            $sheet->setCellValue($column . $row, '\'' . $item['mobile']);

            $column = chr(ord($column) + 1);

            $sheet->setCellValue($column . $row, $item['sex']==1?'男':'女');

            $column = chr(ord($column) + 1);

            $sheet->setCellValue($column . $row, $item['idcard']);

        }

        $writer = new Xlsx($spreadsheet);
        ob_start();
        $writer->save('php://output');
        $ret = ob_get_contents();
        ob_end_clean();

        return response()->withHeaders([
            'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'Content-Disposition' => 'attachment;filename=activity-user.xlsx'
        ])->withBody($ret);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('activity-user/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('activity-user/update');
    }

    protected function formatNormal($items, $total): Response
    {
        $activityIds = array_column($items, 'activity_id');
        $activities = Activity::whereIn('id', $activityIds)->get()->toArray();
        $activities = array_combine(
            array_column($activities, 'id'),
            array_values($activities)
        );

        foreach ($items as $key=>$item) {
            $items[$key]['activity'] = $activities[$item['activity_id']] ?? [];
        }

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items]);
    }
}
